package com.example.tests;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.io.*;

import com.thoughtworks.xstream.XStream;

public class GroupDataGenerator {

	public static void main(String[] args) throws IOException {
		if(args.length < 3){
			System.out.println("Please specify parameters: <tests amount> <file destination> <file format>");
			return;
		}

		int amount = Integer.parseInt(args[0]);
		File file = new File(args[1]);
		String format = args[2];
		
		if(file.exists()){
			System.out.println("File " + file + " is already exists,pls. remove it manually.");
			return;	
		}
		
		List<GroupData> groups = generateRandomGroups(amount);
		if(format.equals("csv")){
			saveGroupsToCsvFile(groups, file);
		} else if(format.equals("xml")){
			saveGroupToXmlFile(groups, file);			
		} else {
			System.out.println("Unknown format " + format);
			return;
		} 		
	}

	public static List<GroupData> generateRandomGroups(int amount) {
		List<GroupData> list = new ArrayList<GroupData>();	
		for (int i = 0; i < amount; i++) {
			GroupData groupInput = new GroupData()
				.withName(generateRandomString())
				.withHeader(generateRandomString())
				.withFooter(generateRandomString());
			list.add(groupInput);
		}
		return list;
	}
	
	public static String generateRandomString(){	
		Random rnd = new Random();		
		char[] chars = new char[]{'+','&','?','-','/','=','_','(',')'};
		//char c = (char)(rnd.nextInt(128-32))+32 ;
		int r = rnd.nextInt(chars.length);
		char randomChar = chars[r];
				
		if (rnd.nextInt(3) == 0) {
			return "";
		} else {
			return "test " + rnd.nextInt() + " " + randomChar ;			
		}
	}
	
	private static void saveGroupToXmlFile(List<GroupData> groups, File file) throws IOException {	
		XStream xstream = new XStream();
		xstream.alias("groups", GroupData.class);
		String xml = xstream.toXML(groups);
		FileWriter writer = new FileWriter(file);
		writer.write(xml);
		writer.close();
	}

	private static void saveGroupsToCsvFile(List<GroupData> groups, File file) throws IOException {
		FileWriter writer = new FileWriter(file);
		for (GroupData group : groups) {
			writer.write(group.getGroup_name() +","+ group.getGroup_header() +","+group.getGroup_footer() + ",!" + "\n");
		}
		writer.close();
	}

	public static List<GroupData> loadGroupsToCsvFile(File file) throws IOException {
		List<GroupData> list = new ArrayList<GroupData>();
		FileReader reader = new FileReader(file);
		BufferedReader bufferedReader = new BufferedReader(reader);
		String line = bufferedReader.readLine();
		while (line != null) {
			String[] part = line.split(",");
			GroupData group = new GroupData()
			.withName(part[0])
			.withHeader(part[1])
			.withFooter(part[2]);
			list.add(group);
			line = bufferedReader.readLine();
		}
		bufferedReader.close();	
		return list;
	}
	
	public static List<GroupData> loadGroupsToXmlFile(File file) throws IOException {
		XStream xstream = new XStream();
		xstream.alias("groups", GroupData.class);
		return (List<GroupData>) xstream.fromXML(file);
	}
	
	

}
